//
//  UIColor+Extension.swift
//  MoyoungLib
//
//  Created by 李然 on 2021/12/10.
//

import Foundation

public extension UIColor {
    
    static func rgb(_ r: CGFloat, _ g: CGFloat, _ b: CGFloat, _ a: CGFloat = 1.0) ->UIColor {
        
        return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
        
    }
    
    static func color(hex: Int, alpha: CGFloat = 1) -> UIColor {
        let mask = 0x000000FF
        let r = Int(hex >> 16) & mask
        let g = Int(hex >> 8) & mask
        let b = Int(hex) & mask
        let red = CGFloat(r) / 255.0
        let green = CGFloat(g) / 255.0
        let blue = CGFloat(b) / 255.0
        return UIColor(red: red, green: green, blue: blue, alpha: alpha)
    }
    
    
    //适配安卓的xml color
    convenience init?(hex: String) {
        
        var hexSanitized = hex.trimmingCharacters(in: .whitespacesAndNewlines)
        hexSanitized = hexSanitized.replacingOccurrences(of: "#", with: "")
        
        var rgb :UInt64 = 0
        
        let length = hexSanitized.count
        
        if Scanner(string: hexSanitized).scanHexInt64(&rgb){
            let r, g, b, a: CGFloat
            
            if length == 6{
                r = CGFloat((rgb & 0x00ff0000) >> 16) / 255
                g = CGFloat((rgb & 0x0000ff00) >> 8) / 255
                b = CGFloat(rgb & 0x000000ff) / 255
                a = 1.0
            }else if length == 8{
                //安卓的透明度在前面两位,暂时注释
                a = CGFloat((rgb & 0xff000000) >> 24) / 255
                r = CGFloat((rgb & 0x00ff0000) >> 16) / 255
                g = CGFloat((rgb & 0x0000ff00) >> 8) / 255
                b = CGFloat(rgb & 0x000000ff) / 255
            }else{
                a = 0
                r = 0
                g = 0
                b = 0
            }
            self.init(red: r, green: g, blue: b, alpha: a)
        }else{
            self.init(red: 0, green: 0, blue: 0, alpha: 0)
        }
    }
    
    static func dynamicProviderColor(dark: String, light: String) ->UIColor? {
        
        guard let lightColor = UIColor.init(hex: light) else {
            return UIColor.init(hex: dark)
        }
        
        guard let darkColor = UIColor.init(hex: dark) else {
            return nil
        }
        
        
        if #available(iOS 13, *) {  // 版本号大于等于13
            
           return UIColor { (traitCollection: UITraitCollection) -> UIColor in
               return traitCollection.userInterfaceStyle == UIUserInterfaceStyle.dark ?
               darkColor : lightColor
           }
        }
         
        return UIColor.init(hex: dark)
            
    }
}


public extension UIColor {
    
     struct DynamicColorModel: Codable {
        
        var dark: String
        var light: String
        
    }
    
    static func colorOfFile(key:String, of filePath: String? = nil) ->UIColor? {
        
        guard let filePath = filePath else {
            return nil
        }
        
        let url = URL(fileURLWithPath: filePath)
        
        guard let data = try? Data(contentsOf: url),  let dict = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] else {
            return nil
        }
        
        
        guard let hex = dict[key] as? String else  {
            
            guard
                let hexs = dict[key] as? [String:String],
                let data = try? JSONSerialization.data(withJSONObject: hexs, options: []),
                let model = try? JSONDecoder().decode(DynamicColorModel.self, from: data) else { return nil }
            return UIColor.dynamicProviderColor(dark: model.dark, light: model.light)
        }
        
        return UIColor.init(hex: hex)
    }
    
}

public extension UIColor {
    
  static func rgb565ToRgba8888(first: UInt8, second: UInt8) -> PixelData {
        var r: UInt8
        var g: UInt8
        var b: UInt8
        var a: UInt8
        
        r = (first >> 3) << 3
        g = ((first << 5) | (second >> 3))
        b = second << 3
        a = UInt8(255)
        return PixelData(r: r, g: g, b: b, a: a)
    }
    
   public struct PixelData {
        var r: UInt8
        var g: UInt8
        var b: UInt8
        var a: UInt8
    }
    
}

